package com.argrace.smart.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.model.LocalDb_RuoQiDeviceTable;
import com.argrace.smart.rokid.RokidMulticastServer;
import com.argrace.smart.ui.EmptyRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/2/23.
 */

public class RuoQi_DeviceListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    EmptyRecycleView recyclerView;
    SwipeRefreshLayout refreshLayout;
    private List<LocalDb_RuoQiDeviceTable> lists = new ArrayList<>();
    RuoQi_DeviceListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ruoqi_devicelist);
        init();
    }
    private void init() {
        Intent intent = new Intent(this, RokidMulticastServer.class);
        startService(intent);
        setView();
        setTitle(true, true, "我的若琪");
        //进入设备修改保存状态界面
        showRight(true, R.mipmap.head_title_add);
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RuoQi_DeviceListActivity.this, RuoQi_AddDeviceActivity.class);
                startActivity(intent);
            }

        });
    }

    @Override
    public void onRefresh() {
        setRefreshing(true);
        lists.clear();
        lists.addAll(DataSupport.findAll(LocalDb_RuoQiDeviceTable.class));
        adapter.notifyDataSetChanged();
        setRefreshing(false);
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    private void setView() {
        recyclerView = (EmptyRecycleView) findViewById(R.id.recyclerView);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RuoQi_DeviceListAdapter(lists, this);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
//        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme));//getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme)
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefresh();
    }

    class RuoQi_DeviceListAdapter extends BaseQuickAdapter<LocalDb_RuoQiDeviceTable, BaseViewHolder> {
        private Context context;

        public RuoQi_DeviceListAdapter(List<LocalDb_RuoQiDeviceTable> data, Context context) {
            super(R.layout.item_ruoqi_devicelist, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, final LocalDb_RuoQiDeviceTable item, final int position) {
            baseViewHolder.setText(R.id.item_ruoqi_devicelist_name, item.getName());
            baseViewHolder.setText(R.id.item_ruoqi_devicelist_type, item.getUuid().split(":")[0]);
//        baseViewHolder.getView(R.id.item_qingjing_name).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                listener.click(item, position);
//            }
//        });
//        baseViewHolder.getView(R.id.item_qingjing_name).setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                longlistener.click(item, position);
//                return true;
//            }
//        });
        }
    }

}
